<?php

class Wiki extends Controller
{
	protected $page = NULL;
	
	protected $data = array(
		'page_title' => 'Chilik-wiki Documentation',
		'page_url' => 'http://localhost/chilik-wiki/',
		'assets_url' => '/assetswiki/',
		'is_active' => 1,
		'username' => 'Chilik-user',
	);
	
	function __construct()
	{
		parent::__construct();
		$this->page = $this->load('model', 'page');
		$this->data['elapsed'] = $this->elapsed();
	}
	
	function add()
	{
		$page = $this->page;
		$page->name = str_replace(' ','',@$_REQUEST['page']);
		$this->data['content'] = '';
		$this->data['page_name'] = $page->name;
		
		View::Factory()->render('edit', $this->data);
	}
	
	function edit()
	{
		echo 'Hello '.@$_REQUEST['page'];
		$page = $this->page;
		$page->db_connect();
		
		$page->name = str_replace(' ','',@$_REQUEST['page']);
		$page->content = $this->page->get_page($page->name);
		
		$page->db_close();
		
		$this->data['page_name'] = $page->name;
		$this->data['content'] = $page->content;
		
		$salt = sha1(rand());
		// $token = ''; kalau ingin dishare
		$token = 'NjA2NjJlM2I3M2M4MWVhOWNkY2YwMDMzZDk3YWRiOGFkNTJiNzczZDA2MzYwYTM3ZjVmZTFmY';
		$token_secret = base64_encode(hash('sha512',$salt.':'.$token));
		
		$this->data['salt'] = $salt;
		$this->data['token'] = $token_secret;
		
		View::Factory()->render('edit', $this->data);
	}
	
	function save()
	{
		$page = $this->page;
		$page->db_connect();
		
		$page->name = str_replace(' ','',@$_REQUEST['page']);
		$page->content = @$_REQUEST['content'];
		$page->created = time();
		$page->modified = time();
		$page->username = 'Chilik-user';
		
		$this->data['page_name'] = $page->name;
		
		$output = $page->save_entry(
						array(
							'name' => $page->name
						), 
						array(
							$page->name, 
							$page->content, 
							$page->created, 
							$page->modified, 
							$page->username
						)
					);
	
		$page->db_close();
		
		header('location: '.$this->data['page_url'].'?page='.$page->name);
	}
	
	function xsearch()
	{
		$page = $this->page;
		$page->db_connect();
		
		$seek = str_replace(' ','',@$_REQUEST['qsearch']);
		if(strlen($seek) <= 0)
		{
			exit();
		}
		
		$page->content = $this->page->get_lists('%'.$seek.'%');
		$page->name = str_replace(' ','',@$_REQUEST['page']);
		
		$page->db_close();
		
		$this->data['page_name'] = $page->name;
		$this->data['content'] = $page->content;
		$this->data['results'] = $page->content;
		$this->data['querys'] = $seek;
		
		View::Factory()->render('result', $this->data);
	}
	
	function search($search)
	{
		$page = $this->page;
		$page->db_connect();
		
		$seek = str_replace(' ','',@$_REQUEST['qsearch']);
		if(strlen($seek) > 0)
		{
			
			$page->content = $this->page->get_lists('%'.$seek.'%');
		}
		else
		{
			$page->content = $this->page->get_lists($search.'%');
		}
		
		$page->name = str_replace(' ','',@$_REQUEST['page']);
		
		$page->db_close();
		
		$this->data['page_name'] = $page->name;
		$this->data['content'] = $page->content;
		
		View::Factory()->render('welcome', $this->data);
	}
	
	function index()
	{
		$page = $this->page;
		
		$page->name = str_replace(' ','',@$_REQUEST['page']);
		$page->name = strlen($page->name) > 0 ? $page->name: 'TOC';
		
		$page->db_connect();
			
		if($page->name === 'TOC')
		{
			$content = $page->get_all_list();
			$html = 'welcome';
		}
		else
		{
			$content = $page->get_page($page->name);
			$html = 'index';
		}
		
		$page->db_close();
	
		$this->data['content'] = $content;
		$this->data['page_name'] = $page->name;
		
		
		View::Factory()->render($html, $this->data);
	}
}

//echo $pagename,'<br/>', $content,'<br/>', strftime("%b %d %Y %X",time()),'\n';
